package com.lykj.modules.shop.mapper;

import com.lykj.modules.shop.entity.UserOrder;
import com.lykj.modules.shop.excel.UserOrderExcel;
import com.lykj.modules.shop.vo.UserOrderVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 现金订单表 Mapper 接口
 *
 * @author cxl
 * @since 2022-01-16
 */
public interface UserOrderMapper extends BaseMapper<UserOrder> {

	/**
	 * 自定义分页
	 *
	 * @param page
	 * @param userOrder
	 * @return
	 */
	List<UserOrderVO> selectUserOrderPage(IPage page, UserOrderVO userOrder);

	/**
	 * 获取导出订单数据
	 *
	 * @param userOrder
	 * @return
	 */
	@Select("<script>SELECT o.* FROM shsy_user_order o where o.is_deleted = 0" +
		"<if test=\"userOrder.id!=null\">" +
		" and o.id = #{userOrder.id}" +
		"</if>" +
		"<if test=\"userOrder.tenantId!=null and userOrder.tenantId!=''\">" +
		" and o.tenant_id = #{userOrder.tenantId}" +
		"</if>" +
		"<if test=\"userOrder.userId!=null\">" +
		" and o.user_id = #{userOrder.userId}" +
		"</if>" +
		"<if test=\"userOrder.payType!=null\">" +
		" and o.pay_type = #{userOrder.payType}" +
		"</if>" +
		"<if test=\"userOrder.cateId!=null\">" +
		" and o.cate_id = #{userOrder.cateId}" +
		"</if>" +
		"<if test=\"userOrder.productId!=null\">" +
		" and o.product_id = #{userOrder.productId}" +
		"</if>" +
		"<if test=\"userOrder.mobile!=null and userOrder.mobile!=''\">" +
		" and o.mobile = #{userOrder.mobile}" +
		"</if>" +
		"<if test=\"userOrder.status!=null\">" +
		" and o.status = #{userOrder.status}" +
		"</if>" +
		"<if test=\"userOrder.account!=null and userOrder.account!=''\">" +
		" and u.account = #{userOrder.account}" +
		"</if>" +
		"<if test=\"userOrder.uid!=null\">" +
		" and u.uid = #{userOrder.uid}" +
		"</if>" +
		"<if test=\"userOrder.startDate!=null and userOrder.endDate!=null\">" +
		" and o.create_time BETWEEN CONCAT(#{userOrder.startDate},' 00:00:00') AND CONCAT(#{userOrder.endDate},' 23:59:59')" +
		"</if>" +
		"</script>")
	List<UserOrderExcel> export(@Param("userOrder") UserOrderVO userOrder);

	/**
	 * 更改处理状态
	 *
	 * @param id
	 */
	@Update("update `shsy_user_order` set is_dispose = 1 where id = #{id}")
	void dispose(@Param("id") Long id);

	@Update("update `shsy_user_order` set delivery_id = #{deliveryId},deliver_time=now(),status=0,delivery_name=#{deliveryName} where id = #{id} and status=-1")
	int setDeliveryName(@Param("id") Long orderId, @Param("deliveryName") String deliveryName, @Param("deliveryId") String deliveryId);

	/**
	 * 停止分红
	 *
	 * @param id
	 */
	@Update("update `shsy_user_order` set stop_bonus = 1 where id = #{id}")
	void stopBonus(@Param("id") Long id);

	/**
	 * 累加分红金额
	 *
	 * @param number
	 * @param id
	 */
	@Update("update `shsy_user_order` set total_bonus = total_bonus+#{number} where id = #{id}")
	void totalBonus(@Param("number") BigDecimal number, @Param("id") Long id);

	/**
	 * 获取订单总额
	 *
	 * @param userId
	 */
	@Select("select if(sum(o.total_price) is null,0,sum(o.total_price)) from shsy_user_order o where o.user_id=#{userId} and o.pay_type in(0,2) and o.`status`=1 and o.is_deleted=0")
	BigDecimal ordersTotal(@Param("userId") Long userId);

	/**
	 * 获取公益订单总额
	 *
	 * @param userId
	 */
	@Select("select if(sum(o.pv_total_price) is null,0,sum(o.pv_total_price)) from shsy_user_order o where o.user_id=#{userId} and o.pay_type in(0,2) and o.`status`=1 and o.is_deleted=0")
	BigDecimal bonusOrdersTotal(@Param("userId") Long userId);

	/**
	 * 获取订单总额
	 */
	@Select("select if(sum(o.pv_total_price) is null,0,sum(o.pv_total_price)) from shsy_user_order o where o.pay_type in(0,2) and o.`status`=1 and o.is_deleted=0")
	BigDecimal sumPayPrice();
}
